package com.example.delegator;
import com.alibaba.fastjson.JSONObject;
import com.example.dto.CategoryNodeInfoViewDTO;
import com.example.vo.ClassificationCheckUniqueParamsVO;
import com.huawei.innovation.rdm.coresdk.basic.util.XDMDelegatorJsonUtil;
import com.huawei.innovation.rdm.coresdk.basic.vo.QueryCondition;
import com.huawei.innovation.rdm.coresdk.basic.vo.QueryRequestVo;
import com.huawei.innovation.rdm.coresdk.basic.vo.RDMPageVO;
import com.huawei.innovation.rdm.xdm.delegator.ClassificationNodeDelegator;
import com.huawei.innovation.rdm.xdm.dto.entity.ClassificationNodeQueryViewDTO;
import com.huawei.innovation.rdm.xdm.dto.entity.ClassificationNodeViewDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class CustomClassificationNodeDelegator {
    private String uri = "rdm/basic/api";
    private String moduleName = "ClassificationNode";

    @Autowired
    private CustomDelegatorService customDelegatorService;
    private String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    public List<ClassificationNodeViewDTO> getChildren(String parentId,boolean enable){
        Map<String,String> map = new HashMap<>();
        map.put("parentId",parentId);
        map.put("enable",enable?"true":"false");
        List list = (List)customDelegatorService.Get(uri,moduleName,"getChildren",map);
        return XDMDelegatorJsonUtil.jsonToDTOList(list, ClassificationNodeViewDTO.class, this.pattern);
    }
    @Autowired
    private ClassificationNodeDelegator classificationNodeDelegator;
    public List<ClassificationNodeViewDTO> getOneLevelClassificationType(boolean enable){
        QueryRequestVo queryRequestVo = new QueryRequestVo();
        QueryCondition queryCondition = new QueryCondition();
        queryRequestVo.setFilter(queryCondition);
        RDMPageVO rdmPageVO = new RDMPageVO();
        List<ClassificationNodeViewDTO> rlist = classificationNodeDelegator.find(queryRequestVo,rdmPageVO);
        List<ClassificationNodeViewDTO> newList = new ArrayList<>();
        for (ClassificationNodeViewDTO e : rlist) {
            if (e.getEnable() == enable && e.getParentNode() == null) {
                newList.add(e);
            }
        }
        rlist = newList; // 将原列表替换为新列表
        return rlist;
    }

    public List<CategoryNodeInfoViewDTO> getCategoryNodeInfo(Long linkId) {
        Map<String,String> map = new HashMap<>();
        map.put("linkId",String.valueOf(linkId));
        List getCategoryNodeInfo = (List)customDelegatorService.Get(uri, moduleName, "getCategoryNodeInfo", map);
        return XDMDelegatorJsonUtil.jsonToDTOList(getCategoryNodeInfo, CategoryNodeInfoViewDTO.class,this.pattern);
    }

    public Boolean checkUnique(ClassificationCheckUniqueParamsVO vo) {
        JSONObject payload = new JSONObject();
        JSONObject o = new JSONObject();
        String key = vo.getKey();
        String value = vo.getValue();
        String parentId = vo.getParentId();
        if(key!=null&&key!=""){
            o.put("key",key);
        }else return false;
        if(value!=null&&value!=""){
            o.put("value",value);
        }else return false;
        if(parentId!=null&&parentId!=""){
            o.put("parentNodeId",parentId);
        }
        payload.put("params",o);
        List checkUnique = (List) customDelegatorService.Post(uri, moduleName, "checkUnique", payload);
        return (Boolean) checkUnique.get(0);
    }
}
